﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;


using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_CourseManager_ManageResource : PowerCreator.LMS.Web.Base.BasePage
{
    protected Course opCourse = new Course();
    
    ManagerPaper opPaper = new ManagerPaper();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";//0简单搜索1高级搜索
            ViewState["OrderList"] = "";

            string courseid = "0";
            string str_classid = "0";

            if (Request.QueryString["DomainID"] != null)
            {
                courseid = Request.QueryString["DomainID"].ToString();
            }
            ViewState["CourseID"] = courseid;

            if (Request.QueryString["ClassID"] != null)
            {
                str_classid = Request.QueryString["ClassID"].ToString();
            }
            ViewState["ClassID"] = str_classid;

            InitPage();
            BinderCoursersType();
          
          
            
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
            BinderResourceClass();
        }
    }

    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lbl_ResourceList_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_ResourceName.Text = GetLanageValueByKey("lbl_ResourceList_ResourceName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CoursersClass.Text = GetLanageValueByKey("lbl_ResourceList_CoursersClass", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CoursersRestp.Text = GetLanageValueByKey("lbl_ResourceList_CoursersRestp", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        btn_Search.Value = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
       
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
       
        VOTOCourse voCourse = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
        lbl_CourseName.Text = string.Format("( {0} 资源)", voCourse.CourseResourceNum.ToString());
        lnkbtn_CourseName.Text = voCourse.CourseName;
        lnkbtn_CourseName.ToolTip = voCourse.Description;

        base.InitPage();
    }
    private void BinderResourceClass()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("Resource_Type", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "-1";
        ddl_CoursersClass.Items.Insert(0, li);
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("Resource_Class1", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "1";
        ddl_CoursersClass.Items.Add(li1);
        ListItem li2 = new ListItem();
        li2.Text = GetLanageValueByKey("Resource_Class2", PowerCreator.LMS.Language.LanagetItemType.Page);
        li2.Value = "2";
        ddl_CoursersClass.Items.Add(li2);
    }
    private void BinderCoursersType()
    {
        DataTable dt = new DataTable();
        string search = "";
        dt = opCourse.QueryResourceType(search);
        ArrayList list = opCourse.GetResourceTypeTree(dt);
        ddl_CoursersRestp.DataSource = list;
        ddl_CoursersRestp.DataTextField = "Text";
        ddl_CoursersRestp.DataValueField = "Value";
        ddl_CoursersRestp.DataBind();

        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ddl_CoursersRestp.Items.Insert(0, li);
    }


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(string.Format("FKCourseresCourseID = {0} and ", ViewState["CourseID"]));

        if (ViewState["SearchType"].ToString() == "1")
        {
            if (ddl_CoursersClass.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format(" CoursersClass = {0} and ", ddl_CoursersClass.SelectedValue.ToString()));
            }

            if (ddl_CoursersRestp.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format(" FKCoursersRestpID = {0} and ", ddl_CoursersRestp.SelectedValue.ToString()));
            }

            if (!string.IsNullOrEmpty(txt_ResourceName.Value))
            {
                sbSearch.Append(string.Format(" FKCoursersResourceID in (select ResourceID from TResource where ResourceName like '%{0}%') and ", txt_ResourceName.Value));
            }
        }
        else if (ViewState["SearchType"].ToString() == "0")
        {
            sbSearch.Append(string.Format(" FKCoursersResourceID in (select ResourceID from TResource where ResourceName like '%{0}%' or ResourceAuthor like '%{0}%' and ResourceDescription like '%{0}%')", txt_SearchKey.Text));
        }

        if (sbSearch.ToString().EndsWith(" and "))
        {
            sbSearch.Remove(sbSearch.Length - 4, 4);
        }

        return sbSearch.ToString();

    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = opCourse.QueryCourseResourceCount(search);
    }
    private void BinderRPT(string search,int pageIndex)
    {
        string orderList = ViewState["OrderList"].ToString();
        DataTable dt = opCourse.QueryCourseResource(search,orderList, LMSPageSize, pageIndex);
        List<CourseResourceInfo> list = FillList(dt);
        rptResource.DataSource = list;
        rptResource.DataBind();
    }


    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        
    }

    //DataTable为多表联链接查询后的数据
    private List<CourseResourceInfo> FillList(DataTable dt)
    {
        List<CourseResourceInfo> list = new List<CourseResourceInfo>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            CourseResourceInfo item = new CourseResourceInfo();
            item.CourseID = int.Parse(dt.Rows[i]["FKCourseresCourseID"].ToString());
            item.CourseresID = int.Parse(dt.Rows[i]["CourseresID"].ToString());
            item.CoursersRestpID = int.Parse(dt.Rows[i]["FKCoursersRestpID"].ToString());
            VOTResourceType voResourceType = opCourse.LoadResourceType(item.CoursersRestpID);
            item.CoursersRestpName = voResourceType.RestpName;
            item.CoursersClassID = int.Parse(dt.Rows[i]["CoursersClass"].ToString());
            item.CoursersClassName = opCourse.GetCoursersClassByData(item.CoursersClassID, GetLanageValueByKey("Resource_Class1", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("Resource_Class2", PowerCreator.LMS.Language.LanagetItemType.Page));

            if (item.CoursersClassID == Convert.ToInt16(CoursersClass.Courseware))  //课件资源
            {
                item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersResourceID"].ToString());
                VOTResource voResource = opCourse.LoadResource(item.CoursersResourceID);
                
                item.ResourceName = voResource.ResourceName;
                item.VOTresourceState = voResource.ResourseState;
            }
            else if (item.CoursersClassID == Convert.ToInt16(CoursersClass.Paper)) //课程资源
            {
                item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersPaperID"].ToString());
                VOPaper voPaper = opPaper.LoadPaper(item.CoursersResourceID);
                item.ResourceName = voPaper.PaperName;
                item.VOTresourceState = 1;
            }

            list.Add(item);
        }
        return list;
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_ServerClick(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";//0简单搜索1高级搜索
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }


    
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "CourseResource.aspx";
        Script.RedirectURL(url);
    }
    protected void rptResource_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "jsyt")
        {
            string url = string.Format("BBS_SysClass.aspx?Type=1&DomainID={0}&ClassID={1}", e.CommandArgument.ToString(), ViewState["ClassID"].ToString());
            Script.RedirectURL(url);

        }
    }
    

    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";//0简单搜索1高级搜索
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
}